#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 与关系型数据库的交互
问题: 你想在关系型数据库中查询、增加或删除记录。
提示 : 
"""

import sqlite3

stocks = [
    ('GOOG', 100, 490.1),
    ('AAPL', 50, 545.75),
    ('FB', 150, 7.45),
    ('HPQ', 75, 33.2),
]

def recipe1():
    # 连接
    db =sqlite3.connect('database.db')
    c = db.cursor()
    
    # 创建表
    c.execute('create table if not exists portfolio (sysmbol text, shares integer, price real)')
    db.commit()

    # 插入记录
    c.executemany('insert into portfolio values (?,?,?)', stocks)

    # 查询
    for row in db.execute('select * from portfolio'):
        print(f"{row = }")

    # 参数查询
    min_price = 100
    for row in db.execute('select * from portfolio where price >=?', (min_price,)):
        print(f"{row = }")
        
def main():
    print('recipe1'.center(20, '*'))
    recipe1()

if __name__ == '__main__':
    main()            

